import json
import os
from py2neo import Graph, Relationship, Node

LOG_CONFIG_FILE = "./config/neo4j.json"
BLOG_ROOT = "./data/blog"
meta_path = os.path.join(BLOG_ROOT, "meta.json")


if __name__ == "__main__":
    with open(meta_path, 'r', encoding='utf-8') as fp:
        obj = json.load(fp)
    

    with open(LOG_CONFIG_FILE, "r", encoding="utf-8") as fp:
        c = json.load(fp)
    config = {
        "profile" : "http:" + c["url"].split(":")[1] + ":8002",
        "name" : c["name"],
        "password" : c["password"]
    }
    g = Graph(**config)

    for blog in obj['blog']:
        blog_name = blog['name']
        blog_tags = blog['tags']
        adjust_time: str = blog['adjustTime']
        adjust_times = adjust_time.split(' ')
        years = adjust_times[0].split('-')
        hours = adjust_times[1].split(':')
        adjust_time = years[0] + "年" + years[1] + "月" + years[2] + "日" + " " + hours[0] + ":" + hours[1]
        cypher = f'match (n:Blog{{name:"{blog_name}"}}) set n.adjustTime="{adjust_time}" return n;'
        cypher = cypher.replace('\'', '\"')
        g.run(cypher)